package com.example.tbag.healthcare.Activity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;

import com.example.tbag.healthcare.Application.MyApplication;
import com.example.tbag.healthcare.R;
import com.example.tbag.healthcare.Util.DatabaseHelper;
import com.example.tbag.healthcare.Util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by tbag on 2017/5/1.
 */

public class LoginActivity extends AppCompatActivity {
    private DatabaseHelper helper;
    private long start = 0;
    @BindView(R.id.edit1)
    EditText account;
    @BindView(R.id.edit2)
    EditText password;
    @BindView(R.id.btn)
    Button button;
    
    @OnClick(R.id.btn)
    public void login(){
        button.setText("登录中...");
        if(account.getText().toString().trim().isEmpty()){
            ToastUtil.show(this,"账号不能为空");
            button.setText("登录");
            return;
        }
        if(password.getText().toString().trim().isEmpty()){
            ToastUtil.show(this,"密码不能为空");
            button.setText("登录");
            return;
        }
        if(query()){
            setResult(0);
            finish();
            return;
        }
        button.setText("登录");
    }
    
    @OnClick(R.id.btn1)
    public void register(){
        startActivityForResult(new Intent(this,RegisterActivity.class),0);
    }

    private boolean query() {
        Cursor c = helper.getReadableDatabase().query("doc",null,"user=? and password = ?",
                new String[]{account.getText().toString(),password.getText().toString()},null,null,null);
        if(c.moveToFirst()){
            ToastUtil.show(this,"登录成功");
            MyApplication.name = c.getString(c.getColumnIndex("name"));
            MyApplication.type = c.getString(c.getColumnIndex("type"));
            //SharePerferenceUtil.getInstance().edit().putString("type",c.getColumnName(c.getColumnIndex("type"))).apply();
            return true;
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - start < 4000) {
            setResult(1);
            finish();
        }else{
            ToastUtil.show(this,"再次点击返回键退出应用");
            start = System.currentTimeMillis();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 0){
            setResult(0);
            finish();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        helper = new DatabaseHelper(this);
    }
}
